package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.ReportMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl  implements ReportService {

//    @Reference
//    private MemberService memberService;

    @Autowired
    private  ReportMapper reportMapper;


    @Override
    //查找去年每个月注册的会员数
    public Map findLastyearMemberCountsByMonth() {
        List<Map> mapList = reportMapper.findLastyearMemberCountsByMonth();
        Map map = changeData(mapList);
        return map;
    }

    @Override
    //得到不同性别的会员数量占比
    public List<Map> getMemberCountsBySex() {
        List<Map> memberCountsList = reportMapper.getMemberCountsBySex();
        return memberCountsList;
    }

    @Override
    //得到各年龄段会员占比
    public List<Map> getMemberCountsByAge() {
        List<Map> memberByageList = new ArrayList<Map>();
        Map countByYear1 = getCountByYear(0, 18);
        memberByageList.add(countByYear1);
        Map countByYear2 = getCountByYear(18, 30);
        memberByageList.add(countByYear2);
        Map countByYear3 = getCountByYear(30, 45);
        memberByageList.add(countByYear3);
        Map countByYear4 = getCountByYear(45,50);
        memberByageList.add(countByYear4);
        return memberByageList;
    }

    @Override
    //套餐占比
    public List<Map> getSetmealProportion() {
        return reportMapper.getSetmealProportion();
    }

    @Override
    //各个月注册的会员数量
    public Map getMemberCountByTime(List<Date> dateList) {
        String startTime = DateUtils.parseDate2String(dateList.get(0), "yyyy-MM") + "-01";
        String endTime = DateUtils.parseDate2String(dateList.get(1), "yyyy-MM") + "-01";
        List<Map> memberListByYear = reportMapper.getMemberCountByTime(startTime, endTime);
        Map map = changeData(memberListByYear);
        return map;
    }

    //日期转换传入年龄段查找会员数量
    private Map changeData(List<Map> mapList) {
         List<String> yearAndMonthList = new ArrayList<String>();
         List<Long> memberCountList = new ArrayList<Long>();
        Map map = new HashMap();
        for (Map map1 : mapList) {
            yearAndMonthList.add((String)map1.get("yearAndMonth"));
            memberCountList.add((Long)map1.get("memberCounts"));
        }
        map.put("memberCounts",memberCountList);
        map.put("yearAndMonth",yearAndMonthList);
        return map;
    }

    //年份转换
    private Map getCountByYear(int beginAge, int endAge) {
        long time = System.currentTimeMillis();
        Date beginDate = new Date(time - beginAge * 1000L * 60 * 60 * 24 * 365);
        Date endDate = new Date(time - endAge * 1000L * 60 * 60 * 24 * 365);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String beginDay = format.format(beginDate);
        String endDay = format.format(endDate);
        Long count = reportMapper.findMemberCount(endDay,beginDay);
        HashMap<Object, Object> memberCountMap = new HashMap<>();
        memberCountMap.put("value",count);
        memberCountMap.put("name",beginAge+"-"+endAge);
        return memberCountMap;
    }



}
